<?php

namespace app\common\model\publics;

use think\Model;
use think\Db;
use app\common\library\Auth;

class Follow extends Model
{
	// 设置当前模型使用的完整数据库连接信息
	protected $connection = 'database.db_second';//连接配置
    // 表名
    protected $name = 'publics_follow';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [

    ];

	public function info($v,$type){

		$user_id = Auth::instance()->id ?: -1;

		if($type == 2){

			$res = Db::connect($this->connection)->name("publics_follow")->where(['user_id'=>$v->user_id,'user2_id'=>$user_id])->find();
			$res2 = Db::connect($this->connection)->name("publics_follow")->where(['user_id'=>$user_id,'user2_id'=>$v->user_id])->find();
			if($res && $res2){
				return 3;
			}
			return $res2 ? 1 : 2;
		}else{
			$res = Db::connect($this->connection)->name("publics_follow")->where(['user_id'=>$v->user_id,'user2_id'=>$user_id])->find();

			if($res){
				return 3;
			}
			return 1;
		}
	}

    public function user()
    {
        return $this->belongsTo('app\common\model\User', 'user2_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
